<?php

namespace App\Http\Middleware\Auth;

use App\Http\Services\LoginService;
use Closure;

class ManageLogin
{
    /**
     * Manage角色鉴权
     */
    public function handle($request, Closure $next)
    {
        //防止非接口调用
        /*if (!$request->get("use", 0) && !preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"])) {
            return retFail("请求失败");
        }*/
        $token = $request->header('Authorization');
        if (empty($token)) {
            return response([
                'code' => 401,
                'msg' => '权限不足',
                'data' => []
            ], 401);
        }

        $token = trim(str_replace("Bearer t1 ", "", $token));
        $login = new LoginService();
        $adminId = $login->authcode(str_replace("t1 ", "", $token), 'DECODE');

        if (empty($adminId)) {
            return response([
                'code' => 401,
                'msg' => '无效的Token',
                'data' => []
            ], 401);
        }
        define("UID", $adminId ?? "");

        return $next($request);
    }
}
